﻿
// init
$(document).ready(function (e) {

    // init progressDialog
    $.progressDialog.init();

    // init messageDialog()
    $.messageDialog.init();

    // init alertDialog
    $.alertDialog.init();

    // init confirmDialog
    $.confirmDialog.init();

    // init historyManager
    $.historyManager.init();

});

// progressDialog
$.progressDialog = {
    // id
    id: "progressDialog",
    // defaults
    defaults: {message: "加载中..." },
    // init method
    init: function (params) {
        var newParams = $.extend(true, {}, $.progressDialog.defaults, params);
        if (window.APPJSPlus == undefined && $("#" + $.progressDialog.id).length == 0) {
            // create DOM
            $(document.body).prepend("<div id='" + $.progressDialog.id + "' class='popdialog' style='display:none;'><div class='popdialog-inner'><div class='dialog'></div></div></div>");
        };
    },
    // open method
    open: function (params) {
        var newParams = $.extend(true, {}, $.progressDialog.defaults, params);
        if(window.APPJSPlus == undefined){
            var objProgressDialog = $("#" + $.progressDialog.id);
            //objProgressDialog.find(".dialog > p").html(newParams.message);
            objProgressDialog.show();
        }else{
            window.APPJSPlus.openProgressDialog(newParams.message);
        };
    },
    // close method
    close: function () {
        if (window.APPJSPlus == undefined) {
            $("#" + $.progressDialog.id).hide();
        } else {
            window.APPJSPlus.closeProgressDialog();
        };
    }
};

// messageDialog ////////////////////////////////////////////
$.messageDialog = {
    // id
    id: "messageDialog",
    // defaults
    defaults: { message: "短消息提示！", duration: "short" },
    // init method
    init: function (params) {
        var newParams = $.extend(true, {}, $.messageDialog.defaults, params);
        if (window.APPJSPlus == undefined && $("#" + $.messageDialog.id).length == 0) {
            // create DOM
            $(document.body).append("<div id='" + $.messageDialog.id + "' style='display:none;'><p>" + newParams.message + "</p></div>");
            // get DOM
            var objMessageDialog = $("#" + $.messageDialog.id);
            objMessageDialog.css({ "position": "fixed", "left": ($(window).width() - objMessageDialog.outerWidth()) / 2 + "px", "bottom": "50px" });
        };
    },
    // open method
    open: function (params) {
        var newParams = $.extend(true, {}, $.messageDialog.defaults, params);
        if (window.APPJSPlus == undefined) {
            var objMessageDialog = $("#" + $.messageDialog.id);
            objMessageDialog.find("p").html(newParams.message);
			objMessageDialog.show();
            if (typeof (newParams.duration) == "string") {
                if (newParams.duration == "long") { newParams.duration = 3000; } else { newParams.duration = 2000; };
            };
            window.setTimeout(function () { objMessageDialog.hide(); }, newParams.duration);
        } else {
            window.APPJSPlus.openMessageDialog(newParams.message, newParams.duration);
        };
    },
};

// alertDialog ///////////////////////////////////////////////////
$.alertDialog = {
    // id
    id: "alertDialog",
    // defaults
    defaults: {title: "提示信息", message: "这里显示提示信息内容！" },
    // okCallback
    okCallback: null,
    // callback 
    callback: function () {
        if (typeof ($.alertDialog.okCallback) == "function") { $.alertDialog.okCallback(); };
        if (window.APPJSPlus == undefined) { $("#" + $.alertDialog.id).hide(); };
    },
    // init method
    init: function (params) {
        var newParams = $.extend(true, {}, $.alertDialog.defaults, params);
        if (window.APPJSPlus == undefined && $("#" + $.alertDialog.id).length == 0) {
            // create DOM
            $(document.body).append("<div id='" + $.alertDialog.id + "' class='popdialog' style='display:none;'><div class='popdialog-inner'><div class='dialog'><div class='dialog-head'><div class='dialog-title'><p>" + newParams.title + "</p></div><div class='dialog-close'><a href='#'>X</a></div></div><div class='dialog-content'><p>" + newParams.message + "</p></div><div class='dialog-button'><input type='button' value='确定' style='width:75%;' /></div></div></div></div>");
            // bind event
            var objAlertDialog = $("#" + $.alertDialog.id);
            objAlertDialog.find(".dialog > .dialog-head > .dialog-close > a").click(function (e) {
                e.preventDefault();
                $.alertDialog.callback();
                objAlertDialog.hide();
            });
            objAlertDialog.find(".dialog > .dialog-button > input").click(function (e) {
                e.preventDefault();
                $.alertDialog.callback();
                objAlertDialog.hide();
            });
        };
    },
    // open method
    open: function (params) {
        var newParams = $.extend(true, {}, $.alertDialog.defaults, params);
        $.alertDialog.okCallback = newParams.callback;
        if (window.APPJSPlus == undefined) {
            var objAlertDialog = $("#" + $.alertDialog.id);
            objAlertDialog.find(".dialog > .dialog-head > .dialog-title > p").html(newParams.title);
            objAlertDialog.find(".dialog > .dialog-content > p").html(newParams.message);
            objAlertDialog.show();
        } else {
            window.APPJSPlus.openAlertDialog(newParams.title, newParams.message, "$.alertDialog.callback();");
        };
    }
};

// confirmDialog /////////////////////////////////////////////
$.confirmDialog = {
    // id
    id: "confirmDialog",
    // defaults
    defaults: {title: "确认信息", message: "这里显示确认信息内容！" },
    // okCallback
    okCallback: null,
    // cancelCallback
    cancelCallback: null,
    // callback 
    callback: function (mode) {
        if (mode == "ok") {
            if (typeof ($.confirmDialog.okCallback) == "function") { $.confirmDialog.okCallback(); };
        } else {
            if (typeof ($.confirmDialog.cancelCallback) == "function") { $.confirmDialog.cancelCallback(); };
        };
        if (window.APPJSPlus == undefined) { $("#" + $.confirmDialog.id).hide(); };
    },
    // init method
    init: function (params) {
        var newParams = $.extend(true, {}, $.confirmDialog.defaults, params);
        if (window.APPJSPlus == undefined && $("#" + $.confirmDialog.id).length == 0) {
            // create DOM
            $(document.body).append("<div id='" + $.confirmDialog.id + "' class='popdialog' style='display:none;'><div class='popdialog-inner'><div class='dialog'><div class='dialog-head'><div class='dialog-title'><p>" + newParams.title + "</p></div><div class='dialog-close'><a href='#'>X</a></div></div><div class='dialog-content'><p>" + newParams.message + "</p></div><div class='dialog-button'><input type='button' value='确定' style='width:40%;' /><input type='button' value='取消' style='width:40%;' /></div></div></div></div>");
            // bind event
            var objConfirmDialog = $("#" + $.confirmDialog.id);
            objConfirmDialog.find(".dialog > .dialog-head > .dialog-close > a").click(function (e) {
                e.preventDefault();
                $.confirmDialog.callback('cancel');
                objConfirmDialog.hide();
            });
            objConfirmDialog.find(".dialog > .dialog-button > input:first").click(function (e) {
                e.preventDefault();
                $.confirmDialog.callback('ok');
                objConfirmDialog.hide();
            });
            objConfirmDialog.find(".dialog > .dialog-button > input:last").click(function (e) {
                e.preventDefault();
                $.confirmDialog.callback('cancel');
                objConfirmDialog.hide();
            });
        };
    },
    // open method
    open: function (params) {
        var newParams = $.extend(true, {}, $.confirmDialog.defaults, params);
        $.confirmDialog.okCallback = newParams.okCallback;
        $.confirmDialog.cancelCallback = newParams.cancelCallback;
        if (window.APPJSPlus == undefined) {
            var objConfirmDialog = $("#" + $.confirmDialog.id);
            objConfirmDialog.find(".dialog > .dialog-head > .dialog-title > p").html(newParams.title);
            objConfirmDialog.find(".dialog > .dialog-content > p").html(newParams.message);
            objConfirmDialog.show();
        } else {
            window.APPJSPlus.openConfirmDialog(newParams.title, newParams.message, "$.confirmDialog.callback('ok');", "$.confirmDialog.callback('cancel');");
        };
    }
};

// history back
$.historyManager = {
    // id
    id: "historyManager",
    // defaults
    defaults: { title: "后退" },
    // init method
    init: function (params) {
        var newParams = $.extend(true, {}, $.historyManager.defaults, params);
        if (window.APPJSPlus == undefined && $("#" + $.historyManager.id).length == 0) {
            // create DOM
            $(document.body).append("<a id='" + $.historyManager.id + "' style='display:none;' href='#' title='" + newParams.title + "'>&lt;</a>");
            var objHistoryManager = $("#" + $.historyManager.id);
            objHistoryManager.click(function(e){
                e.preventDefault();
                window.history.back();
            });
        };
    },
    // open method
    open: function (params) {
        var newParams = $.extend(true, {}, $.historyManager.defaults, params);
        if (window.APPJSPlus == undefined) {
            var objHistoryManager = $("#" + $.historyManager.id);
            objHistoryManager.attr("title", newParams.title);
            objHistoryManager.show();
        };
    },
    // history back
    historyBack: function () {
        if (window.APPJSPlus == undefined) {
            window.history.back();
        } else {
            window.APPJSPlus.historyBack();
        };
    }
};
